package de.hs.rm.UJAdapter.event.impl.jmx;

import javax.management.MBeanServerNotification;
import javax.management.Notification;
import javax.management.NotificationListener;
import org.osgi.framework.BundleContext;
import de.hs.rm.UJAdapter.UJAdapter;

public class UJEasyRegisterNotificationListener implements NotificationListener {
	
	private UJAdapter adapter = null;
	private BundleContext context = null;
	
	public UJEasyRegisterNotificationListener(UJAdapter adapter, BundleContext context)
	{
		this.adapter = adapter;
		this.context = context;
	}
	
	@Override
	public void handleNotification(Notification notification,
            Object handback)
	{
		if (!(notification instanceof MBeanServerNotification)) {
             System.out.println("Ignored notification of class " + notification.getClass().getName());
             return;
         }

		if (notification.getType().equals(MBeanServerNotification.REGISTRATION_NOTIFICATION))
			try {
				adapter.removeUPnPDevice(context);
				adapter.createUPnPDevice(context);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		else if (notification.getType().equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION))
			try {
				adapter.removeUPnPDevice(context);
				adapter.createUPnPDevice(context);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    
	}

}
